package com.imageim.yimirror.ui.adapter;

import android.content.Context;
import android.graphics.Color;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.imageim.yimirror.R;
import com.imageim.yimirror.bean.IntelligenAnalysisBean;
import com.imageim.yimirror.common.ui.GlideUtils;
import com.imageim.yimirror.common.util.ClickUtil;
import com.makeramen.roundedimageview.RoundedImageView;

import java.util.List;

/**
 * Created by Yuaihen.
 * on 2018/6/27
 * 历史分析界面Adapter(原智能分析)
 */
public class ClothingAnalysisViewAdapter extends RecyclerView.Adapter {
    private int blue = Color.parseColor("#e6ff00a8");

    private Context mContext;
    List<IntelligenAnalysisBean.ClothingResultBean> mData;
    private OnItemClickListener mOnItemClickListener;

    public ClothingAnalysisViewAdapter(Context context, List<IntelligenAnalysisBean.ClothingResultBean> list) {
        mContext = context;
        if (list != null) {
            mData = list;
        }
    }


    public void setData(List<IntelligenAnalysisBean.ClothingResultBean> list) {
        if (list != null) {
            mData = list;
            notifyDataSetChanged();
        }

    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(mContext).inflate(R.layout.item_clothing_analysis, null);
        return new IntelligenAnalysisHolder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull final RecyclerView.ViewHolder holder, final int position) {
        final IntelligenAnalysisHolder clothingAnalysisHolder = ((IntelligenAnalysisHolder) holder);

        final IntelligenAnalysisBean.ClothingResultBean bean = mData.get(position);
        if (bean != null) {
            GlideUtils.display(clothingAnalysisHolder.ivPhoto, bean.getUrl());
        }

        if (mOnItemClickListener != null) {
            clothingAnalysisHolder.ivPhoto.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    setSelect(position);
                    mOnItemClickListener.onItemClick(position);
                }
            });

            clothingAnalysisHolder.ivDelete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (ClickUtil.isValidClick()) {
                        if (mData != null) {
                            //                        //先移除数据
                            //                        mData.remove(position);
                            //                        //显示动画效果
                            //                        notifyItemRemoved(position);
                            //                        //重新排序
                            //                        notifyItemRangeChanged(0, mData.size());
                            //通知界面调用删除接口
                            mOnItemClickListener.onDeleteClick(position);
                        }
                    }
                }
            });

        }

        if (bean != null) {
            if (bean.getChecked()) {
                clothingAnalysisHolder.ivPhoto.setBorderColor(blue);
            } else {
                clothingAnalysisHolder.ivPhoto.setBorderColor(Color.TRANSPARENT);
            }
        }

    }

    /**
     * 设置选中的item
     *
     * @param position 选中item的position
     */
    private void setSelect(int position) {
        if (mData == null || mData.size() <= 0) {
            return;
        }

        for (int i = 0; i < mData.size(); i++) {
            if (i == position) {
                mData.get(i).setChecked(true);
            } else {
                mData.get(i).setChecked(false);
            }
        }
        notifyDataSetChanged();
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getItemCount() {
        return mData == null ? 0 : mData.size();
    }

    static class IntelligenAnalysisHolder extends RecyclerView.ViewHolder {
        private RoundedImageView ivPhoto;
        private ImageView ivDelete;
        //        private View selectView;

        public IntelligenAnalysisHolder(View itemView) {
            super(itemView);
            ivPhoto = itemView.findViewById(R.id.iv_photo);
            ivDelete = itemView.findViewById(R.id.iv_delete);
            //            selectView = itemView.findViewById(R.id.select_view);
        }
    }

    public interface OnItemClickListener {
        void onItemClick(int position);

        void onDeleteClick(int position);

    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.mOnItemClickListener = onItemClickListener;
    }
}
